package com.company.cloud.user.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.user.modules.base.service.TeamInfoService;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.UserInvitationUpdateParam;
import com.company.cloud.user.modules.mgr.service.UserInvitationMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.modules.base.entity.UserInvitationInfo;
import com.company.cloud.user.modules.base.service.UserInvitationInfoService;
import com.company.cloud.user.modules.cache.models.UserInvitationModel;
import com.company.cloud.user.modules.mgr.controller.req.UserInvitationQuery4PageParam;
import com.company.cloud.user.modules.mgr.controller.req.UserInvitationSaveParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

import java.util.function.Function;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/admin/userInvitation")
@Api(tags="User_Mgr_用户邀请管理")
public class UserInvitationMgrController {
	
	@Autowired
	private UserInvitationInfoService userInvitationInfoService;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private TeamInfoService teamInfoService;
	@Autowired
	private UserInvitationMgrService userInvitationMgrService;

	@PostMapping("/query")
	@ApiOperation(value="查询用户邀请", response=UserInvitationModel.class)
	public BaseResult query(@RequestBody UserInvitationQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<UserInvitationInfo> page = userInvitationInfoService.page(
				new Page<>(params.getPage(), params.getRows()),
						new QueryWrapper<UserInvitationInfo>()
							.eq("rows_state",  BaseConstant.EnabledEnum.ENABLED.getCode())
							.eq("company_no", userInfo.getCompanyNo())
							.eq(StringUtils.hasText(params.getUserTypeOne()), "user_type_one", params.getUserTypeOne())
							.eq(StringUtils.hasText(params.getSourceOne()), "source_one", params.getSourceOne())
							.like(StringUtils.hasText(params.getNickName()), "nick_name", params.getNickName())
							.like(StringUtils.hasText(params.getRealName()), "real_name", params.getRealName())
							.eq(StringUtils.hasText(params.getMobile()), "mobile", params.getMobile())
							.eq(StringUtils.hasText(params.getInvitationState()), "invitation_state", params.getInvitationState())
							.orderByDesc("cdt")
				);
		IPage<UserInvitationModel> data= page.convert(new Function<UserInvitationInfo, UserInvitationModel>() {
			@Override
			public UserInvitationModel apply(UserInvitationInfo entity) {
				return UserConvertUtils.getUserInvitationModel(entity);
			}
		});
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@StaffLogc(title = "User_Mgr_用户邀请管理 、新增用户邀请", action = "User_Mgr_用户邀请管理、新增用户邀请", actionAdo = "User_Mgr_用户邀请管理")
	@ApiOperation(value="新增用户邀请" ,response =BaseResult.class)
	public BaseResult save(@RequestBody UserInvitationSaveParam params) {
		userInvitationMgrService.save(params);
		return BaseResult.ok();
	}

	@PostMapping("/edit")
	@StaffLogc(title = "User_Mgr_用户邀请管理 、修改用户邀请", action = "User_Mgr_用户邀请管理、修改用户邀请", actionAdo = "User_Mgr_用户邀请管理")
	@ApiOperation(value="修改用户邀请" ,response =BaseResult.class)
	public BaseResult edit(@RequestBody UserInvitationUpdateParam params) {
		userInvitationMgrService.update(params);
		return BaseResult.ok();
	}

	@GetMapping("/get")
	@ApiOperation(value="获取用户邀请", response=UserInvitationModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		UserInvitationInfo entity = userInvitationInfoService.getById(id);
		if (entity == null || BaseConstant.EnabledEnum.ENABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		return BaseResult.ok(UserConvertUtils.getUserInvitationModel(entity));
	}
	
	@GetMapping("/delete")
	@StaffLogc(title = "User_Mgr_用户邀请管理 、删除用户邀请", action = "User_Mgr_用户邀请管理、删除用户邀请", actionAdo = "User_Mgr_用户邀请管理")
	@ApiOperation(value="删除用户邀请", response=BaseResult.class)
	public BaseResult delete(@RequestParam("id")Integer id) {
		LoginSimpleUserInfo user = AuthContextUtils.getUser();
		UserInvitationInfo entity = userInvitationInfoService.getById(id);
		if (entity == null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
		entity.addEditParam(user);
		userInvitationInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
	


}
